<template>
  <el-dialog
    :title="this.title"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="600px"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="100px"
      :disabled="this.flag"
    >
      <el-form-item label="日程标题" prop="title">
        <el-input
          v-model.trim="dataForm.title"
          placeholder="请输入标题"
          maxlength="15"
          show-word-limit
          clearable
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item label="起始时间" prop="startTime">
        <el-date-picker
          v-model="dataForm.startTime"
          type="datetime"
          placeholder="选择日期时间"
          :editable="false"
          :clearable="false"
          format="yyyy-MM-dd HH:mm"
          value-format="timestamp"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="dataForm.endTime"
          type="datetime"
          placeholder="选择日期时间"
          :editable="false"
          :clearable="false"
          format="yyyy-MM-dd HH:mm"
          value-format="timestamp"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="记录内容" prop="content">
        <el-input
          v-model="dataForm.content"
          placeholder="记录你将要做的一件事..."
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <!-- <el-form-item label="提醒设置">
        <el-input-number
          v-model="dataForm.early"
          placeholder="默认1小时"
          controls-position="right"
        />
      </el-form-item> -->
      <!-- <el-form-item label="提醒方式">
        <el-checkbox v-model="appAlert">APP提醒</el-checkbox>
        <el-checkbox v-model="weChatAlert">微信提醒</el-checkbox>
        <el-checkbox v-model="mailAlert">邮件提醒</el-checkbox>
        <el-checkbox v-model="mobileAlert">短信提醒</el-checkbox>
      </el-form-item> -->
      <el-form-item label="标签颜色">
        <el-color-picker
          v-model="dataForm.colour"
          :predefine="[
            '#188ae2',
            '#35b8e0',
            '#26bf8c',
            '#f9c851',
            '#ff5b5b',
            '#5b69bc',
            '#ff8acc',
            '#3b3e47',
            '#282828',
          ]"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer" v-if="!this.flag">
      <el-button type="danger" @click="handleDel()" v-if="dataForm.id"
        >删 除</el-button
      >
      <el-button @click="visible = false">{{
        $t("common.cancelButton")
      }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">
        {{ $t("common.confirmButton") }}</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import {
  ScheduleInfo,
  ScheduleUpdate,
  ScheduleCreate,
  ScheduleDelete,
} from "@/api/extend/schedule";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("结束时间不能为空"));
      } else {
        if (this.dataForm.startTime >= value) {
          callback(new Error("结束时间应大于起始时间"));
        }
        callback();
      }
    };
    return {
      visible: false,
      btnLoading: false,
      dataForm: {
        id: 0,
        title: "",
        startTime: "",
        endTime: "",
        content: "",
        colour: "#188ae2",
      },
      flag: false,
      title: "",
      dataRule: {
        startTime: [
          { required: true, message: "起始时间不能为空", trigger: "change" },
        ],
        endTime: [
          { required: true, validator: validatePass, trigger: "change" },
        ],
        content: [
          { required: true, message: "记录内容不能为空", trigger: "blur" },
        ],
        title: [
          { required: true, message: "日程标题不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id, startTime, flag) {
      if (id && !flag) {
        this.title = "编辑日程";
      } else if (flag) {
        this.title = "日程详情";
      } else {
        this.title = "添加日程";
      }
      this.flag = flag;
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          ScheduleInfo(this.dataForm.id).then((res) => {
            this.dataForm = res.data;
            // this.appAlert = this.dataForm.appAlert ? true : false;
            // this.mailAlert = this.dataForm.mailAlert ? true : false;
            // this.weChatAlert = this.dataForm.weChatAlert ? true : false;
            // this.mobileAlert = this.dataForm.mobileAlert ? true : false;
          });
        } else {
          this.dataForm.startTime = startTime || "";
        }
      });
    },
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          this.dataForm.appAlert = this.appAlert ? 1 : 0;
          this.dataForm.mailAlert = this.mailAlert ? 1 : 0;
          this.dataForm.weChatAlert = this.weChatAlert ? 1 : 0;
          this.dataForm.mobileAlert = this.mobileAlert ? 1 : 0;
          const formMethod = this.dataForm.id ? ScheduleUpdate : ScheduleCreate;
          if (!this.dataForm.id) delete this.dataForm.id;
          formMethod(this.dataForm)
            .then((res) => {
              if (res.msg == "duplication") {
                this.$message.warning("时间冲突，请重新选择！");
                return;
              } else {
                this.$message({
                  message: res.msg,
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.btnLoading = false;
                    this.$emit("refreshDataList");
                  },
                });
              }
            })
            .catch(() => {
              this.btnLoading = false;
            });
        }
      });
    },
    handleDel() {
      this.$confirm("您确定要删除当前日程吗, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          ScheduleDelete(this.dataForm.id).then((res) => {
            this.$message({
              message: res.msg,
              type: "success",
              duration: 1500,
            });
            this.visible = false;
            this.$emit("refreshDataList");
          });
        })
        .catch(() => {});
    },
  },
};
</script>
